package com.edu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.User;
import com.edu.entity.goodsCollection;
import com.edu.service.impl.collectionServiceImpl;

/**
 * Servlet implementation class addCollectionServlet
 */
@WebServlet(name = "addCollDo", urlPatterns = { "/addCollDo" })
public class addCollectionServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		collectionServiceImpl csi = new collectionServiceImpl();
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("logInUser");
		int userId = u.getUserid();
		String getGoodsId = request.getParameter("goodId");
		int goodId = Integer.parseInt(getGoodsId);
		String getUserId = request.getParameter("userId");
		int sellerId = Integer.parseInt(getUserId);
		System.out.println("1:"+userId);
		System.out.println("2:"+goodId);
		System.out.println("3:"+sellerId);
		goodsCollection gc = csi.findColl(userId, goodId);
		System.out.println(gc);
		boolean collC = false;
		if(gc != null && gc.getCollUserid() > 0) {
			collC = true;
		}else {
			collC = false;
		}
		request.setAttribute("collC", collC);
		boolean add = request.getParameter("add") != null;
		boolean del = request.getParameter("del") != null;
		if(add) {
			int out1 = csi.addCollection(userId, goodId);
			if(out1 > 0) {
				System.out.println("添加成功！用户id为："+u.getUserid()+"商品id为："+goodId);
			}else {
				System.out.println("添加失败！");
			}
		}else if(del) {
			boolean out2 = csi.deleteCollection(userId, goodId);
			if(out2) {
				System.out.println("删除成功！用户id为："+u.getUserid()+"商品id为："+goodId);
			}else {
				System.out.println("删除失败！");
			}
		}
		
		request.getRequestDispatcher("goodsInfo?goodId="+goodId+"&userId="+sellerId).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		collectionServiceImpl csi = new collectionServiceImpl();
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("logInUser");
		int userId = u.getUserid();
		String getGoodsId = request.getParameter("goodid");
		int goodId = Integer.parseInt(getGoodsId);
		String getUserId = request.getParameter("userid");
		int sellerId = Integer.parseInt(getUserId);
		boolean add = request.getParameter("add") != null;
		boolean del = request.getParameter("del") != null;
		boolean collC = false;
		request.setAttribute("collC", collC);
		if(add) {
			int out1 = csi.addCollection(userId, goodId);
			if(out1 > 0) {
				System.out.println("添加成功！用户id为："+u.getUserid()+"商品id为："+goodId);
				collC = true;
			}else {
				System.out.println("添加失败！");
			}
		}else if(del) {
			boolean out2 = csi.deleteCollection(userId, goodId);
			if(out2) {
				System.out.println("删除成功！用户id为："+u.getUserid()+"商品id为："+goodId);
				collC = false;
			}else {
				System.out.println("删除失败！");
			}
		}
		request.setAttribute("collC", collC);
		request.getRequestDispatcher("goodsInfo?goodId="+goodId+"&userId="+sellerId).forward(request, response);
	}

}
